﻿#pragma warning(disable:4200)

#include "QStyle.h"
#include "awtk.h"

const char *STYLE_TEXT[] = {
    "bg_color",
    "fg_color",
    "mask_color",
    "font_name",
    "font_size",
    "font_style",
    "text_color",
    "highlight_font_name",
    "highlight_font_size",
    "highlight_text_color",
    "tips_text_color",
    "text_align_h",
    "text_align_v",
    "border_color",
    "border_width",
    "border",
    "bg_image",
    "bg_image_draw_type",
    "icon",
    "fg_image",
    "fg_image_draw_type",
    "spacer",
    "margin",
    "margin_left",
    "margin_right",
    "margin_top",
    "margin_bottom",
    "icon_at",
    "active_icon",
    "x_offset",
    "y_offset",
    "selected_bg_color",
    "selected_fg_color",
    "selected_text_color",
    "round_radius",
    "round_radius_top_left",
    "round_radius_top_right",
    "round_radius_bottom_left",
    "round_radius_bottom_right",
};

const char *STATE_TEXT[] = {
    "",
    "normal",
    "changed",
    "pressed",
    "over",
    "disable",
    "focused",
    "checked",
    "unchecked",
    "empty",
    "empty_focus",
    "error",
    "selected",
    "normal_of_checked",
    "pressed_of_checked",
    "over_of_checked",
    "disable_of_checked",
    "focused_of_checked",
    "normal_of_active",
    "pressed_of_active",
    "over_of_active",
    "disable_of_active",
    "focused_of_active",
};

//copy from enums.c
const char *toIconPos(int pos)
{
    if (pos == STYLE_ICON_LEFT)
        return "left";
    else if (pos == STYLE_ICON_RIGHT)
        return "right";
    else if (pos == STYLE_ICON_TOP)
        return "top";
    else if (pos == STYLE_ICON_BOTTOM)
        return "bottom";
    else
        return "cent";
}

const char *toDrawType(int type)
{
    if (type == STYLE_DRAW_SCALE)
        return "scale";
    else if (type == STYLE_DRAW_REPATE)
        return "repeat";
    else
        return "default";
};

void QStyle::getAlign(int option, int &text_align_v, int &text_align_h)
{    
    if (option & Qt::AlignTop)
        text_align_v = ALIGN_V_TOP;
    else if (option & Qt::AlignVCenter)
        text_align_v = ALIGN_V_MIDDLE;
    else if (option & Qt::AlignBottom)
        text_align_v = ALIGN_V_BOTTOM;

    if (option & Qt::AlignLeft)
        text_align_h = ALIGN_H_LEFT;
    else if (option & Qt::AlignHCenter)
        text_align_h = ALIGN_H_CENTER;
    else if (option & Qt::AlignRight)
        text_align_h = ALIGN_H_RIGHT;
}


QString QStyle::styleSheet()
{
    return m_style;
}

void QStyle::expandState(QString &signal, int state)
{
    QString ret;
    if (state == 0)
        ret = signal;
    else
    {
        int flag = 1;
        for (int i = 1; i < 32; i++)
        {            
            if (state & flag)
            {
                if (!ret.isEmpty())
                    ret += ";";

                ret += QString(STATE_TEXT[i]) + ":" + signal;
            }
            flag = flag << 1;
        }
    }

    if (!m_style.isEmpty())
        m_style += ";";
    m_style += ret;
}

void QStyle::text_color(const QColor &c, int state)
{
    color(STYLE_TEXT_COLOR, c, state);
}

void QStyle::bg_color(const QColor &c, int state)
{
    color(STYLE_BG_COLOR, c, state);    
}

void QStyle::color(int style_id, const QColor &c, int state)
{
    QString color = QString().sprintf("#%02x%02x%02x%02x", c.red(), c.green(), c.blue(), c.alpha());
    QString style = QString(STYLE_TEXT[style_id]) + "=" + color;
    expandState(style, state);
}

void QStyle::bg_image(QString path,int type, int state)
{    
    QString style = QString(STYLE_TEXT[STYLE_BG_IMAGE]) + "=" + path;
    expandState(style, state);

    style = QString(STYLE_TEXT[STYLE_BG_IMAGE_DRAW_TYPE]) + "=" + toDrawType(type);
    expandState(style, state);
}

void QStyle::icon(QString path, int type, int state)
{
    QString style = QString(STYLE_TEXT[STYLE_ICON]) + "=" + path;
    expandState(style, state);

    style = QString(STYLE_TEXT[STYLE_ICON_AT]) + "=" + toIconPos(type);
    expandState(style, state);
}

void QStyle::font_name(QString name, int state )
{
    QString style = QString(STYLE_TEXT[STYLE_FONT_NAME]) + "=" + name;
    return expandState(style, state);
}

void QStyle::font_size(int size, int state)
{
    QString style = QString(STYLE_TEXT[STYLE_FONT_SIZE]) + "=" + QString::number(size);
    return expandState(style, state);
}

void QStyle::textAlign(int align, int state)
{
    QString style;
    return expandState(style, state);
}

void QStyle::style(int stype_id, const QString &text, int state)
{
    QString style = QString(STYLE_TEXT[stype_id]) + "=" + text;
    return expandState(style, state);
}

void style_init()
{

}